<?php

/**
 * 聊天室控制类使用GatewayWorker
 */
namespace app\index\controller;
use       think\Controller;
use       think\Request;
use       think\Db;
use       GatewayClient\Gateway;
use       think\Cache;
use       think\Session;

class Gatewaywork extends Controller
{
    public function _initialize(){

    }
    public function bind(Request $request)
    {
        // 用户连接websocket之后,绑定uid和clientid,同时进行分组,根据接收到的roomid进行分组操作
        $Data=Request::instance()->post();
        // 接受到上面的三个参数,进行分组操作
        Gateway::$registerAddress = '120.25.242.117:1238';
        // client_id与uid绑定
        // Gateway::bindUid($clientId, $userGuid);
        // 加入某个群组（可调用多次加入多个群组） 将clientid加入roomid分组中
        Gateway::joinGroup($Data['client_id'], $Data['room_id']);
        // 返回ajax json信息
        $dataArr=[
            'status'=>true,
            'message'=>'Group Success'
        ];
        return json($dataArr);
    }
    // 接受用户的信息 并且发送
    public function send(Request $request){
        //获得数据
        $Data=Request::instance()->post();
        Gateway::$registerAddress = '120.25.242.117:1238';
        //根据用户的User_id获取用户的头像和昵称
        $List=Db::table('x_weixin_user')->where('id',$Data['UserId'])->find();
		//语音文件
        if($Data['Type']==2)
        {
            $SendData=[
           'Message' =>$Data['Message'],
           'NickName'=>$List['nickname'],
           'HeadPic' =>$List['headimgurl'],
           'Type'    =>$Data['Type'],
           'FromUserId'=>$Data['UserId'],
           'Time'   =>$Data['Time'],
           'T'      =>$Data['T'],
           'number' =>$Data['number'],
           'sort'   =>$Data['sort']
        ];
           
            //添加音频信息到数据库
            $StorageData=[
            'room_id'    =>$Data['RoomId'],
            'tem_voice'  =>$Data['Message'],
            't'          =>$Data['T'],
            'number'     =>$Data['number'],
            'sort'       =>$Data['sort'],
            'create_time'=>time()
            ];
            Db::table('x_voice_list')->insert($StorageData);
        }
		else if($Data['Type']==3)
		{
			$SendData=[
            'Message' =>$Data['Message'],
            'NickName'=>$List['nickname'],
            'HeadPic' =>$List['headimgurl'],
            'Type'    =>$Data['Type'],
            'FromUserId'=>$Data['UserId'],
            'TimeY'   =>date('Y-m-d'),
            'TimeM'   =>date('G:i:s'),
            ];
		}
        else{
        $SendData=[
        'Message' =>$Data['Message'],
        'NickName'=>$List['nickname'],
        'HeadPic' =>$List['headimgurl'],
        'Type'    =>$Data['Type'],
        'FromUserId'=>$Data['UserId'],
        'TimeY'   =>date('Y-m-d'),
        'TimeM'   =>date('G:i:s'),
        ];

            //添加文字信息到数据库
            $StorageData=[
            'room_id'    =>$Data['RoomId'],
            'user_id'    =>$Data['UserId'],
            'content'    =>$Data['Message'],
            'create_time'=>time()
            ];
            if($Data['UserId']!=$Data['RoomUserId'])
            Db::table('x_text_list')->insert($StorageData);
        }
         // 向roomId的分组发送数据
         Gateway::sendToGroup($Data['RoomId'],json_encode($SendData));
    }
}

?>